local MAX = GetModConfigData("wortox_ex")
local Btn = GetModConfigData("wortox_ex_btn")


-- 默认是关的，方便上下洞穴保留魂
local allow_drop = false

local function GetStackSize(item)
    return item.replica.stackable and item.replica.stackable:StackSize() or 1
end

local function TheDrop(inst)
    if not allow_drop then return end
    local soul = GetItemFromAll("wortox_soul")
    if soul then
        local count = GetStackSize(soul)
        if count > MAX and inst.replica.inventory then
            inst.replica.inventory:DropItemFromInvTile(soul)
        end
    end
end

local function OnWorldPostInit(inst)
    inst:ListenForEvent("playeractivated", function()
        local player = GLOBAL.ThePlayer
        if player and player.prefab == "wortox" then
            player:DoPeriodicTask(0.5, function()
                TheDrop(player)
            end)
        end
    end)
end

AddPrefabPostInit("world", OnWorldPostInit)

GLOBAL.TheInput:AddKeyUpHandler(Btn, function ()
    if InGame() and GLOBAL.ThePlayer.prefab == "wortox" then 
        allow_drop = not allow_drop
        TIP("防止爆魂","yellow", allow_drop)
    end
end)
